# coding=utf8

from __future__ import print_function

import sys
import io
import numpy as np

import pickle

reload(sys)
sys.setdefaultencoding('utf8')

datdir = '/Users/vista/PycharmProjects/data/movie_comments/'

def getlex():
    f = io.open(datdir + 'lexcion.pickle', 'rb')
    lex = pickle.load(f)
    f.close()
    return lex

class DataSet(object):
    def __init__(self, features, shape):

        self._num_examples = features.shape[0]

        self._features = features
        self._epochs_completed = 0
        self._index_in_epoch = 0

        self._shape = shape

    @property
    def shape(self):
        return self._shape

    def get_x_labels(self):

        feavet = np.zeros([self._num_examples,self._shape])
        for row in range(self._num_examples):
            for i in range(len(self._features[row][0])):
                feavet[row][self._features[row][0][i]] = 1
        self._x = feavet

        self._labels = np.array([x[1] for x in self._features[:]])


    @property
    def x(self):
        return self._x

    @property
    def labels(self):
        return self._labels

    @property
    def num_examples(self):
        return self._num_examples

    @property
    def epochs_completed(self):
        return self._epochs_completed

    def set_index(self,v):
        self._index_in_epoch = v

    def next_batch(self, batch_size):
        """Return the next `batch_size` examples from this data set."""
        start = self._index_in_epoch

        if(start == 0):
            perm = np.arange(self._num_examples)
            np.random.shuffle(perm)
            self._features = self._features[perm]

        self._index_in_epoch += batch_size
        if self._index_in_epoch > self._num_examples:
            # Finished epoch
            self._epochs_completed += 1
            # Shuffle the data
            perm = np.arange(self._num_examples)
            np.random.shuffle(perm)
            self._features = self._features[perm]
            # Start next epoch
            start = 0
            self._index_in_epoch = batch_size
            assert batch_size <= self._num_examples
        end = self._index_in_epoch
        feavet = np.zeros([batch_size,self._shape])
        for row in range(batch_size):
            for i in range(len(self._features[start+row][0])):
                feavet[row][self._features[start+row][0][i]] = 1

        return feavet, np.array([x[1] for x in self._features[start:end]])


def load_vec(shape):
    with io.open(datdir + 'les2.train.pickle', 'rb') as f:
        train_dat = pickle.load(f)
        f.close()

    with io.open(datdir + 'les2.test.pickle', 'rb') as f:
        test_dat = pickle.load(f)
        f.close()

    # read train data
    class DataSets(object):
        pass

    data_sets = DataSets()

    data_sets.train = DataSet(train_dat,shape)
    data_sets.test = DataSet(test_dat,shape)

    return data_sets